/*
 * Copyright 2017 the original author or authors.
 *
 */

package com.ivo.core.spring;

import java.lang.annotation.Annotation;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * <h1>SpringContext工具</h1>
 * <p>
 * </p>
 * <b>Note:</b>
 *
 * @author nexf
 * @since 2015/10/2 13:46
 */
public class SpringContext implements ApplicationContextAware {

  private static Logger logger = LoggerFactory.getLogger(SpringContext.class);

  private static ApplicationContext applicationContext;

  public static ApplicationContext getApplicationContext() {
    return applicationContext;
  }

  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    SpringContext.applicationContext = applicationContext;
    logger.info("容器 {} 加载完成", applicationContext.getDisplayName());

  }

  public static Object getBean(String name) throws BeansException {
    return applicationContext.getBean(name);
  }

  public static <T> Map<String, T> getBeans(Class<T> type) throws BeansException {
    return applicationContext.getBeansOfType(type);
  }

  public static <T> T getBean(Class<T> type) throws BeansException {
    return applicationContext.getBean(type);
  }

  public static String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType)
      throws BeansException {
    return applicationContext.getBeanNamesForAnnotation(annotationType);
  }
}
